Public Class MyPrintPreviewDialog
    Inherits PrintPreviewDialog

    Const REGAPP = "WRDBDGV"
    Private components As System.ComponentModel.IContainer
    ' print document 
    Private printDoc As System.Drawing.Printing.PrintDocument = Nothing

    'extend PrintPreviewDialog, adding context menu and ability to press ESC to close
    'because a Page Setup call is allowed, generally call like this: 
    '
    '   Dim MyPreviewDlg As New MyPrintPreviewDialog
    '        With MyPreviewDlg
    '            .Document = pd
    '            Do While .ShowDialog = DialogResult.Retry : Loop
    '        End With

    Sub New(ByRef _PrintDoc As System.Drawing.Printing.PrintDocument)

        MyBase.New()

        ' create print document 
        If _PrintDoc Is Nothing Then printDoc = New System.Drawing.Printing.PrintDocument() Else printDoc = _PrintDoc

        MyBase.Document = _PrintDoc

        'add context menu
        Dim eh As New EventHandler(AddressOf mnuPreview_Click)
        Dim mnuPreviewPrint As New ToolStripMenuItem("&Print...", My.Resources.PrintHS, eh, Keys.Control Or Keys.P) : mnuPreviewPrint.ToolTipText = "Show printer dialog box and then print"
        Dim mnuPreviewQuickPrint As New ToolStripMenuItem("&Quick Print", My.Resources.NavForward, eh) : mnuPreviewQuickPrint.ToolTipText = "Immediately send to current printer"
        Dim mnuPreviewPrintPDF As New ToolStripMenuItem("&Create PDF", My.Resources.PDF, eh) : mnuPreviewPrintPDF.ToolTipText = "Create PDF file and display"
        Dim mnuPreviewPageSetup As New ToolStripMenuItem("Page &Setup...", My.Resources.PrintSetupHS, eh) : mnuPreviewPageSetup.ToolTipText = "Display printer and page setup dialog"
        Dim mnuPreviewClose As New ToolStripMenuItem("&Close", My.Resources.DeleteHS, eh) : mnuPreviewClose.ToolTipText = "Close this print preview form (or can press ESC key)"
        Dim ContextMenuPreview As New ContextMenuStrip
        ContextMenuPreview.Items.AddRange(New System.Windows.Forms.ToolStripItem() {mnuPreviewPrint, mnuPreviewQuickPrint, mnuPreviewPrintPDF, New ToolStripSeparator, mnuPreviewPageSetup, mnuPreviewClose})
        MyBase.ContextMenuStrip = ContextMenuPreview

        'add key handler
        AddHandler MyBase.KeyPress, AddressOf PreviewKeyPress
        MyBase.KeyPreview = True

        AddHandler MyBase.Closed, AddressOf SavePosition

        'better form title
        MyBase.Text = "Print Preview"

        MyBase.PrintPreviewControl.Zoom = GetSetting(REGAPP, "PrintPreview", "Zoom", 1.0)
        MyBase.PrintPreviewControl.AutoZoom = GetSetting(REGAPP, "PrintPreview", "AutoZoom", True)

        Dim tt As New ToolTip
        tt.SetToolTip(MyBase.PrintPreviewControl, "Right-click for options...")

        'allow remembered startup position
        MyBase.StartPosition = FormStartPosition.Manual
        GetWindowPos(REGAPP, "", MyBase.Left, MyBase.Top, MyBase.Width, MyBase.Height, MyBase.WindowState)
    End Sub

    Private Sub SavePosition(ByVal sender As Object, ByVal e As System.EventArgs)
        SaveWindowPos(REGAPP, "", MyBase.Left, MyBase.Top, MyBase.Width, MyBase.Height, MyBase.WindowState)
        SaveSetting(REGAPP, "PrintPreview", "Zoom", MyBase.PrintPreviewControl.Zoom)
        SaveSetting(REGAPP, "PrintPreview", "AutoZoom", MyBase.PrintPreviewControl.AutoZoom)
    End Sub

    Public Sub mnuPreview_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim Caption As String = CType(sender, ToolStripMenuItem).Text.Replace("...", "").Replace("&", "")
        Select Case Caption
            Case "Print"
                Dim pd As New PrintDialog
                pd.Document = printDoc
                pd.AllowSelection = True
                pd.AllowSomePages = True
                pd.AllowCurrentPage = True
                pd.AllowPrintToFile = False
                If pd.ShowDialog = Windows.Forms.DialogResult.OK Then MyBase.DialogResult = Windows.Forms.DialogResult.OK : MyBase.Close()
            Case "Quick Print" : MyBase.DialogResult = Windows.Forms.DialogResult.OK : MyBase.Close()
            Case "Page Setup" : If PageSetup() Then MyBase.DialogResult = DialogResult.Retry
            Case "Create PDF" : MyBase.DialogResult = Windows.Forms.DialogResult.Abort : MyBase.Close()
            Case "Close" : MyBase.DialogResult = Windows.Forms.DialogResult.Cancel : MyBase.Close()
        End Select
    End Sub

    Private Sub PreviewKeyPress(ByVal sender As Object, ByVal e As System.windows.forms.KeyPressEventArgs)
        If e.KeyChar = Chr(27) Then MyBase.Close()
    End Sub

    Friend Function PageSetup() As Boolean
        Dim frm As New PageSetupDialog
        With frm
            .AllowPaper = False
            .Document = printDoc
            Return .ShowDialog() = Windows.Forms.DialogResult.OK
        End With
    End Function

    Private Sub InitializeComponent()
        Me.SuspendLayout()
        '
        'MyPrintPreviewDialog
        '
        Me.ClientSize = New System.Drawing.Size(400, 300)
        Me.Name = "MyPrintPreviewDialog"
        Me.ShowIcon = False
        Me.ResumeLayout(False)

    End Sub
End Class
